//
// Created by denglibin on 2021/6/2.
//

#include "main_scene.h"
#include "global.h"
#include "stdio.h"
/**
 * 事件处理
 * @param gameWindow
 */
static void handleEvent(MainScene* mainScene);

/**
 * 绘制地图
 * @param mainScene
 */
static void drawMap(MainScene* mainScene);
/**
 * 创建
 * @param gameWindow
 * @return
 */
MainScene* MainSceneCreate(GameWindow* gameWindow){
    MainScene* mainScene = (MainScene*)malloc(sizeof(MainScene));
    mainScene->gameWindow = gameWindow;
    mainScene->ground_h = 20;
    mainScene->map_w = 2000;
    mainScene->start_x = 0;
    //设置精灵坐标
    mainScene->gameWindow->mainSpirit->y = mainScene->gameWindow->win_h - mainScene->ground_h - mainScene->gameWindow->mainSpirit->h;
    mainScene->gameWindow->mainSpirit->x = 0;
    mainScene->gameWindow->mainSpirit->map_x = 0;
    mainScene->gameWindow->mainSpirit->map_y = mainScene->gameWindow->mainSpirit->y;
    mainScene->gameWindow->mainSpirit->map_w = mainScene->map_w;
    return mainScene;
}
/**
 * 绘制场景
 * @param gameWindow
 */
void MainSceneDraw(MainScene* mainScene){
    if(mainScene == NULL || mainScene->gameWindow == NULL){
        return;
    }
    //事件处理
    handleEvent(mainScene);
    //设置渲染器颜色
    SDL_SetRenderDrawColor(mainScene->gameWindow->winRender, 0, 0, 0, 0);
    SDL_RenderClear(mainScene->gameWindow->winRender); //用设置的颜色清除
    SDL_SetRenderDrawBlendMode(mainScene->gameWindow->winRender, SDL_BLENDMODE_BLEND);//设置混合模式，支持alpha透明

    //绘制精灵
    GlobalDrawSpirit(mainScene->gameWindow, mainScene->gameWindow->mainSpirit);
    //绘制地图
    drawMap(mainScene);
    //生效
    SDL_RenderPresent(mainScene->gameWindow->winRender);
    SDL_ShowWindow(mainScene->gameWindow->win);
}
/**
 * 释放资源
 * @param mainScene
 */
void MainSceneFree(MainScene* mainScene){
    free(mainScene);
}

/**
 * 绘制地图
 * @param mainScene
 */
void drawMap(MainScene* mainScene){
    GameWindow * gameWindow = mainScene->gameWindow;
    MainSpirit * mainSpirit = mainScene->gameWindow->mainSpirit;
    if(mainSpirit->map_x <= gameWindow->win_w/2){
        mainScene->start_x = 0;
    }else if(mainSpirit->map_x > mainScene->map_w - gameWindow->win_w/2){
        mainScene->start_x = mainScene->map_w - gameWindow->win_w;
    }else{
        mainScene->start_x =  mainSpirit->map_x - gameWindow->win_w/2;
    }

    int w = 50, g;
    SDL_Rect distRect = {0, mainScene->gameWindow->win_h - mainScene->ground_h, w, mainScene->ground_h};
    for(int i = 0; i < mainScene->map_w/w; i++){
        g = i % 2 == 0 ? 255 : 128;
        distRect.x = i * w - mainScene->start_x;
        if(distRect.x < - w ){
            continue;
        }
        if(distRect.x >= mainScene->gameWindow->win_w){
            break;
        }
        //设置渲染器颜色
        SDL_SetRenderDrawColor(mainScene->gameWindow->winRender, g, g, g, 255);
        SDL_RenderFillRect(mainScene->gameWindow->winRender, &distRect);
    }


}

void handleEvent(MainScene* mainScene){
    //GameWindow * gameWindow = mainScene->gameWindow;
    MainSpirit * mainSpirit = mainScene->gameWindow->mainSpirit;
    while (SDL_PollEvent(&(mainScene->gameWindow->event))){
        GlobalHandleEvent(mainScene->gameWindow); //全局的事件处理
        if(mainScene->gameWindow->event.type == SDL_KEYDOWN){
            switch (mainScene->gameWindow->event.key.keysym.sym) {
                case SDLK_a:
                    mainSpirit->move_direction = -1;
                    break;
                case SDLK_d:
                    mainSpirit->move_direction = 1;
                    break;
                case SDLK_SPACE:
                    MainSpiritJump(mainSpirit);
                    break;
                default:
                    break;
            }
        }
        if(mainScene->gameWindow->event.type == SDL_KEYUP) {
            switch (mainScene->gameWindow->event.key.keysym.sym) {
                case SDLK_a:
                case SDLK_d:
                    mainSpirit->move_direction = 0;
                    break;
                default:
                    break;
            }

        }
    }
}